package com.easybenefit.UUService.activity;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.fragment.ContactFragment;
import com.easybenefit.UUService.fragment.CouponsListFragment;
import com.easybenefit.UUService.fragment.MerchantsIntroduceFragment;
import com.easybenefit.UUService.model.HttpMerchantBean;
import com.easybenefit.UUService.model.HttpMerchantsInfoBean;
import com.easybenefit.UUService.model.MerchantsInfoBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

public class MerchantsDetailActivity extends Activity implements IActivityInterface, View.OnClickListener {
    private Button btn_back, btn_add;//返回和发布
    private TextView tv_title;
    private TextView tv_left;
    private TextView tv_center;
    private TextView tv_right;
    private ContactFragment contactFragment;
    private CouponsListFragment couponsListFragment;
    private MerchantsIntroduceFragment merchantsIntroduceFragment;
    private Context context;
    private String id;
    private MerchantsInfoBean merchantsInfoBean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_merchants_detail);
        initView();
        initData();
        initListener();
    }

    @Override
    public void initData() {
        context=this;
        id=getIntent().getStringExtra("id");
        getMerchantsData();
        contactFragment = ContactFragment.newInstance();
        couponsListFragment = CouponsListFragment.newInstance();
        couponsListFragment.setId(id);
        merchantsIntroduceFragment = MerchantsIntroduceFragment.newInstance();

    }

    @Override
    public void initView() {
        btn_back = (Button) findViewById(R.id.finish_btn);
        tv_title = (TextView) findViewById(R.id.top_title);
        btn_add = (Button) findViewById(R.id.btn_add);
        tv_left = (TextView) findViewById(R.id.tv_left);
        tv_right = (TextView) findViewById(R.id.tv_right);
        tv_center = (TextView) findViewById(R.id.tv_center);
        tv_left.setTextColor(getResources().getColor(R.color.smallblue));
    }

    @Override
    public void initListener() {
        btn_back.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_left.setOnClickListener(this);
        tv_center.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        FragmentTransaction transaction = getFragmentManager().beginTransaction();

        switch (v.getId()) {
            case R.id.finish_btn:
                finish();
                break;
            case R.id.tv_left:
                tv_left.setTextColor(getResources().getColor(R.color.smallblue));
                tv_center.setTextColor(getResources().getColor(R.color.grey));
                tv_right.setTextColor(getResources().getColor(R.color.grey));

                transaction.replace(R.id.container, merchantsIntroduceFragment);
                break;
            case R.id.tv_center:
                tv_left.setTextColor(getResources().getColor(R.color.grey));
                tv_center.setTextColor(getResources().getColor(R.color.smallblue));
                tv_right.setTextColor(getResources().getColor(R.color.grey));

                transaction.replace(R.id.container, couponsListFragment);
                break;
            case R.id.tv_right:
                tv_left.setTextColor(getResources().getColor(R.color.grey));
                tv_center.setTextColor(getResources().getColor(R.color.grey));
                tv_right.setTextColor(getResources().getColor(R.color.smallblue));

                transaction.replace(R.id.container, contactFragment);
                break;
        }
        transaction.commit();
    }
    public void getMerchantsData() {
        if (!UUNetUtill.isConnected(this)) {
            UUToastUtil.showToast(this, UUDict.NOT_INTERNET);
        } else {
            String url = UUConfig.SERVER_BASE_URL + "coopinfo";
            RequestParams params = new RequestParams();
            params.put("mer_id", id);
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {

                }

                @Override
                public void onFinish() {

                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpMerchantsInfoBean httpMerchantsInfoBean = (HttpMerchantsInfoBean) UUJsonUtil.fromJson(content, HttpMerchantsInfoBean.class);
                    merchantsInfoBean=httpMerchantsInfoBean.getData();
                    merchantsIntroduceFragment.setMerchantsInfoBean(merchantsInfoBean);
                    couponsListFragment.setMerchantsInfoBean(merchantsInfoBean);
                    contactFragment.setMerchantsInfoBean(merchantsInfoBean);
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    transaction.replace(R.id.container, merchantsIntroduceFragment);
                    transaction.commit();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });
        }
    }
}
